package vn.orderonline.core.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import vn.orderonline.core.dao.common.GenericDaoImpl;
import vn.orderonline.core.dao.entity.MenuDrink;

@Repository
public class MenuDrinkDaoImpl extends GenericDaoImpl<MenuDrink, Integer>
		implements MenuDrinkDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> getListMenuDrinkByResId(Integer resId) {
		String hql = "Select res.restaurantId, res.restaurantName, md.menuDrinkId, md.menuDrinkName "
				+ "From Restaurant res, RestaurantMenuDrink rmd, MenuDrink md "
				+ "Where res.restaurantId = rmd.id.restaurantId And rmd.id.menuDrinkId = md.menuDrinkId "
				+ "And res.restaurantId = :restaurantId";
		Query query = getSession().createQuery(hql);
		query.setParameter("restaurantId", resId);
		return query.list();
	}

	@Override
	public Boolean checkExistedMenuDrinkName(Integer resId, String menuDrinkName) {
		String hql = "From Restaurant res, RestaurantMenuDrink rmd, MenuDrink md "
				+ "Where res.restaurantId = rmd.id.restaurantId And "
				+ "rmd.id.menuDrinkId = md.menuDrinkId And "
				+ "res.restaurantId = :restaurantId And md.menuDrinkName = :menuDrinkName";
		Query query = getSession().createQuery(hql);
		query.setParameter("restaurantId", resId);
		query.setParameter("menuDrinkName", menuDrinkName);
		return !query.list().isEmpty() ? true : false;
	}

	@Override
	public String getMenuDrinkNameByMenuDrinkId(Integer menuDrinkId) {
		Criteria criteria = getSession().createCriteria(MenuDrink.class);
		criteria.setProjection(Projections.property("menuDrinkName"));
		criteria.add(Restrictions.eq("menuDrinkId", menuDrinkId));
		return (String) criteria.uniqueResult();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<String> getMenuDrinkNameByResId(Integer resId) {
		String hql = "Select md.menuDrinkName "
				+ "From Restaurant r, RestaurantMenuDrink rd, MenuDrink md "
				+ "Where r.restaurantId = rd.id.restaurantId And rd.id.menuDrinkId = md.menuDrinkId And "
				+ "r.restaurantId = :restaurantId";
		Query query = getSession().createQuery(hql);
		query.setParameter("restaurantId", resId);
		return query.list();
	}

}
